package com.aliothservice.catfish.product.data;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Update;

public class Warehouse {
	
	public static Update updateFromWarehousePending(WarehousePending pending) {
		Update update = new Update();
		
		update.set("userId", pending.getUserId());
		update.set("name", pending.getName());
		update.set("owner", pending.getOwner());
		update.set("ownerPhoneNumber", pending.getOwnerPhoneNumber());
		update.set("province", pending.getProvince());
		update.set("city", pending.getCity());
		update.set("district", pending.getDistrict());
		update.set("address", pending.getAddress());
		update.set("temperatureLevels", pending.getTemperatureLevels());
		update.set("foundDate", pending.getFoundDate());
		update.set("warehousePhotoId", pending.getWarehousePhotoId());
		update.set("warehouseInsidePhotoId", pending.getWarehouseInsidePhotoId());
		update.set("manInChargeName", pending.getManInChargeName());
		update.set("manInChargePhoneNumber", pending.getManInChargePhoneNumber());
		update.set("lastUpdateTime", new Date());
		
		return update;
	}
	
	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("id", this.getId());
		map.put("name", this.getName());
		map.put("owner", this.getOwner());
		map.put("ownerPhoneNumber", this.getOwnerPhoneNumber());
		map.put("province", this.getProvince());
		map.put("city", this.getCity());
		map.put("district", this.getDistrict());
		map.put("address", this.getAddress());
		map.put("temperatureLevels", this.getTemperatureLevels());
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		map.put("foundDate", formatter.format(this.getFoundDate()));
		map.put("warehousePhotoId", this.getWarehousePhotoId());
		map.put("warehouseInsidePhotoId", this.getWarehouseInsidePhotoId());
		map.put("manInChargeName", this.getManInChargeName());
		map.put("manInChargePhoneNumber", this.getManInChargePhoneNumber());
		map.put("status", this.getStatus());
		if (null != this.getDescriptionOfReject()) {
			map.put("descriptionOfReject", this.getDescriptionOfReject());
		}
		DateFormat fullFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		map.put("lastUpdateTime", fullFormatter.format(this.getLastUpdateTime()));
		if (null != this.getDeviceSN()) {
			map.put("deviceSN", this.getDeviceSN());
		}
		if (null != this.getLocation()) {
			map.put("longitude", Double.valueOf(this.getLocation().getX()));
			map.put("latitude", Double.valueOf(this.getLocation().getY()));
		}
		
		return map;
	}
	
	public Map<String, Object> toSearchableMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("name", this.getName());
		map.put("province", this.getProvince());
		map.put("city", this.getCity());
		map.put("district", this.getDistrict());
		map.put("address", this.getAddress());
		map.put("temperatureLevels", this.getTemperatureLevels());
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		map.put("foundDate", formatter.format(this.getFoundDate()));
		map.put("warehousePhotoId", this.getWarehousePhotoId());
		map.put("warehouseInsidePhotoId", this.getWarehouseInsidePhotoId());
		map.put("manInChargeName", this.getManInChargeName());
		if (this.getManInChargePhoneNumber().length() != 11) {
			map.put("manInChargePhoneNumber", this.getManInChargePhoneNumber());
		} else {
			String maskedPhoneNumber = String.format("%s*****%s", this.getManInChargePhoneNumber().substring(0, 2), this.getManInChargePhoneNumber().substring(7, 10));
			map.put("manInChargePhoneNumber", maskedPhoneNumber);
		}
		if (null != this.getIndividual()) {
			map.put("individual", this.getIndividual());
		}
		if (null != this.getEnterprise()) {
			map.put("enterprise", this.getEnterprise());
		}
		if (null != this.getDeviceSN()) {
			map.put("deviceSN", this.getDeviceSN());
		}
		
		return map;
	}
	
	public static final String WAREHOUSE_STATUS_APPROVED = "approved";
	public static final String WAREHOUSE_STATUS_REJECTED = "rejected";
	
	@Id
	private String id;
	
	// who owns this warehouse
	private String userId;
	
	private String name;
	private String owner;
	private String ownerPhoneNumber;
	private String province;
	private String city;
	private String district;
	private String address;
	private List<Map<String,Integer>> temperatureLevels;
	private Date foundDate;
	
	// links to file stored in GridFS
	private String warehousePhotoId;
	private String warehouseInsidePhotoId;
	
	private String manInChargeName;
	private String manInChargePhoneNumber;
	
	private String status;
	private String descriptionOfReject;
	
	private String individual;
	private String enterprise;
	
	private Date lastUpdateTime;
	
	private String deviceSN;
	
	private GeoJsonPoint location;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getOwnerPhoneNumber() {
		return ownerPhoneNumber;
	}

	public void setOwnerPhoneNumber(String ownerPhoneNumber) {
		this.ownerPhoneNumber = ownerPhoneNumber;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public List<Map<String, Integer>> getTemperatureLevels() {
		return temperatureLevels;
	}

	public void setTemperatureLevels(List<Map<String, Integer>> temperatureLevels) {
		this.temperatureLevels = temperatureLevels;
	}

	public Date getFoundDate() {
		return foundDate;
	}

	public void setFoundDate(Date foundDate) {
		this.foundDate = foundDate;
	}

	public String getWarehousePhotoId() {
		return warehousePhotoId;
	}

	public void setWarehousePhotoId(String warehousePhotoId) {
		this.warehousePhotoId = warehousePhotoId;
	}

	public String getManInChargeName() {
		return manInChargeName;
	}

	public void setManInChargeName(String manInChargeName) {
		this.manInChargeName = manInChargeName;
	}

	public String getManInChargePhoneNumber() {
		return manInChargePhoneNumber;
	}

	public void setManInChargePhoneNumber(String manInChargePhoneNumber) {
		this.manInChargePhoneNumber = manInChargePhoneNumber;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getDescriptionOfReject() {
		return descriptionOfReject;
	}

	public void setDescriptionOfReject(String descriptionOfReject) {
		this.descriptionOfReject = descriptionOfReject;
	}

	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getWarehouseInsidePhotoId() {
		return warehouseInsidePhotoId;
	}

	public void setWarehouseInsidePhotoId(String warehouseInsidePhotoId) {
		this.warehouseInsidePhotoId = warehouseInsidePhotoId;
	}

	public String getIndividual() {
		return individual;
	}

	public void setIndividual(String individual) {
		this.individual = individual;
	}

	public String getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(String enterprise) {
		this.enterprise = enterprise;
	}

	public String getDeviceSN() {
		return deviceSN;
	}

	public void setDeviceSN(String deviceSN) {
		this.deviceSN = deviceSN;
	}

	public GeoJsonPoint getLocation() {
		return location;
	}

	public void setLocation(GeoJsonPoint location) {
		this.location = location;
	}

}
